package com.antler.smt.portal.service.impl;

import com.antler.smt.constant.OrderConstant;
import com.antler.smt.entity.OrderEntity;
import com.antler.smt.entity.RequirementEntity;
import com.antler.smt.portal.mapper.OrderEntityMapper;
import com.antler.smt.portal.mapper.RequirementEntityMapper;
import com.antler.smt.response.base.Result;
import com.antler.smt.response.portal.SupplierShowTimeResp;
import com.antler.smt.service.portal.RequirementService;
import com.antler.smt.service.portal.SupplierShowTimeService;
import com.antler.smt.utils.DateUtil;
import org.apache.catalina.LifecycleState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhangyi
 * @date 2018/5/18 18:22
 */
@Service
public class SupplierShowTimeServiceImpl implements SupplierShowTimeService {

    @Autowired
    private OrderEntityMapper orderEntityMapper;
    @Autowired
    private RequirementEntityMapper requirementEntityMapper;

    /**
     * 获取此订单下的预定时间或者实际交货时间
     *
     * @param orderId
     * @return
     */
    @Override
    public Result getSupplierShowTime(Long orderId) {
        Result result = null;

        OrderEntity orderEntity = orderEntityMapper.selectByPrimaryKey(orderId);
        //支付审核时间
        Date payAuditTime = orderEntity.getPayAuditTime();

        RequirementEntity requirementEntity = requirementEntityMapper.selectByPrimaryKey(orderEntity.getRequirementId());

        //获取交期/小时
        Integer endline = requirementEntity.getEndline();

        Long endTime = new Long(endline*60*60*1000);

        //预定时间：交期+项目专员审核金额后的时间
        SupplierShowTimeResp supplierTime1 = new SupplierShowTimeResp();
        supplierTime1.setTime(DateUtil.stampToDate(endTime+payAuditTime.getTime()));
        supplierTime1.setType(1);

        //实际交货时间：采购商收货的时间
        SupplierShowTimeResp supplierTime2 = new SupplierShowTimeResp();
        if(OrderConstant.ORDER_WAIT_COMMENT <= orderEntity.getStatus() && !orderEntity.getStatus().equals(OrderConstant.ORDER_FAIL)){
            supplierTime2.setTime(orderEntity.getUpdateTime());
        }
        supplierTime2.setType(2);

        List<SupplierShowTimeResp> list = new ArrayList<>();
        list.add(supplierTime1);
        list.add(supplierTime2);
        result = new Result(list);
        return result;
    }
}
